package com.ruoyi.test.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ExcelListener extends AnalysisEventListener<ExcelDTO> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelListener.class);

    private List<ExcelDTO> dataList = new ArrayList<ExcelDTO>();

    public List<ExcelDTO> getDataList() {
        return dataList;
    }
    @Override
    public void invoke(ExcelDTO data, AnalysisContext analysisContext) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
        data.setId(dataList.size() + 2);
        dataList.add(data);
        // 存储完成清理 list
        //dataList.clear();
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        LOGGER.info("所有数据解析完成！");
    }
}
